import logging

import pytest

from resource.pom.gallery import Gallery
from utils.manager import MatchRules


@pytest.fixture(scope='function', autouse=True)
def func_setup_teardown(device):
    logging.info('------用例前置：预置视频资源------')
    app = Gallery
    device.mediatool_delete_all()
    device.hdc_file_send(app.video1, app.videos_dir)
    device.mediatool_send(app.videos_dir)
    yield
    logging.info('------用例后置：删除预置资源------')
    device.mediatool_delete_all()


class Test:
    """
    1.进入视频，点击选择一个视频
2.点击删除按钮，选择删除
3.查看视频结果

1.可以正常进入到视频
2.显示“是否删除此视频”的提示，点击删除成功
3.视频库中此视频已不存在
    """
    app = Gallery

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.进入视频，点击选择一个视频')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.refresh_layout()
        device.click_element(device.get_element_by_key('TabPhotos'))
        device.refresh_layout()
        video = device.get_element_by_key('ImageGridFocus_file', rule=MatchRules.Contain)
        device.click_element(device.get_element_by_key('Gesture_1'))
        logging.info('预期结果1.可以正常进入到视频')
        device.refresh_layout()
        device.assert_key_exist('ActionBarButtonBack')
        device.assert_key_exist('PhotoBrowserDateTitle')
        device.assert_key_exist('PhotoBrowserTimeLocationTitle')
        device.assert_key_exist('DetailMenuPanel')
        device.assert_key_exist('browserFocus_file', MatchRules.Contain)
        device.assert_key_exist('VideoPlayButton')
        device.assert_key_exist('ToolBarButtonFavor')
        device.assert_text_exist('收藏')
        device.assert_key_exist('ToolBarButtonEdit')
        device.assert_text_exist('编辑')
        device.assert_key_exist('ToolBarButtonDelete')
        device.assert_text_exist('删除')
        device.assert_key_exist('ToolBarButtonMore')
        device.assert_text_exist('更多')

        logging.info('步骤2.点击删除按钮，选择删除')
        device.click_element(device.get_element_by_key('ToolBarButtonDelete'))
        logging.info('预期结果2.显示“是否删除此视频”的提示，点击删除成功')
        device.refresh_layout()
        device.assert_type_exist('Dialog')
        device.assert_text_exist('是否删除此视频？')
        device.assert_text_exist('取消')
        device.assert_text_exist('删除')
        device.click_element(device.get_element_by_key('DeleteDialogConfirmButton'))
        logging.info('步骤3.查看视频结果')
        device.refresh_layout()
        logging.info('预期结果3.视频库中此视频已不存在')
        if device.get_element_by_key('EmptyPhotoOrAlbumIco'):
            return
        exist_videos = device.get_elements_by_key('ImageGridFocus_file', rule=MatchRules.Contain)
        if not exist_videos:
            return
        for v in exist_videos:
            if v.get('key') == video.get('key'):
                raise AssertionError('视频删除失败')